import { getStorage, setStorage } from "../../utils/localStorage"
import { townName_obtain } from "../../api/homePage"
import { baseUrl } from "../../api/request"
const app = getApp();
Component({
  data: {
    navHeight: 0,
    navTop: 0,
    navObj: 0,
    navWeight: 0,
    navIndex: 2,
    userInfo: null,
    baseUrl: null,
    navData: {},
    townName: null, //乡镇名
    //路线nav导航栏索引
    // lxNavIndex: 0,
    //商品导航栏索引
    spNavIndex: 1,
    backNum: 1,
  },
  properties: {
    data: {
      type: Object,
      value: {
        title: '首页', //导航栏名称
        level: 1 //导航栏级别
      },
      observer: function (val) {
        this.setData({
          navData: val
        });
      }
    },
    syIndex: {
      type: Number,
      value: 2
    },
    spNavIndex: {
      type: Number,
      value: 1
    },
    //路线导航栏参数
    lxNavIndex: {
      type: Number,
      value: 0
    },
    //后退页数
    backDelta: {
      type: Number,
      value: 1,
    },
    //乡镇名
    townName: {
      type: String,
      value: "",
      observer: function (val) {
        let that = this;
        that.setData({
          townName: val.slice(val.lastIndexOf(",") + 1, val.length)
        });
      }
    }
  },
  lifetimes: {
    attached() {
      this.setData({ navIndex: this.properties.syIndex, userInfo: getStorage('user'), });
      this.getNavInfo();
    },
    detached() { },
  },
  pageLifetimes: {
    show() {
    }
  },
  methods: {
    //获取导航栏信息
    getNavInfo() {
      this.setData({
        navHeight: app.globalData.navHeight, //导航栏高度
        navTop: app.globalData.navTop, //导航栏距顶部距离
        navObj: app.globalData.navObj, //胶囊的高度
        navWeight: app.globalData.navWeight, //胶囊宽度+距右距离
      });
    },
    //切换栏change事件
    navQh(e) {
      const index = e.currentTarget.dataset.index;
      this.setData({
        navIndex: index
      });
      this.triggerEvent('navEvent', index)
    },
    //商品切换栏点击事件
    navSp(e) {
      const index = e.currentTarget.dataset.index;
      this.setData({
        spNavIndex: index
      });
      this.triggerEvent('spNavEvent', index)
    },
    //搜索点击事件
    search(e) {
      wx.navigateTo({
        url: '/pagesShouYe/pages/search/index',
      })
    },
    //返会上一页
    back() {
      if (this.data.navData.title === "核销扫码" || this.data.navData.title === "扫码签到") {
        wx.redirectTo({
          url: '/pages/index/index',
        });
      } else if (this.data.navData.title === "动态详情" || this.data.navData.title === "智能规划" || this.data.navData.title === "规划完成") {
        this.triggerEvent("eventBack");
      } else {
        app.globalData.detailPageType = this.data.data.title;
        if (app.globalData.sheetFlag) {
          wx.redirectTo({
            url: '/pages/index/index',
          });
        } else {
          wx.navigateBack({
            delta: this.data.backDelta || 1
          })
        };

      }
    },
    //搜索框箭头事件
    searchBack() {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    },
    //活动公共头部日历图标点击事件
    rili() {
      wx.navigateTo({
        url: '/pagesHuodong/pages/riqi/index',
      })
    },
    //搜索框事件
    searchBlur(e) {
      const val = e.detail.value;
      this.triggerEvent("searchEvent", val);
    },
    //banner详情公共头部收藏事件
    async collection(e) {
      const obj = e.currentTarget.dataset.item;
      let status = obj.colleStatus === 0 ? 1 : obj.colleStatus === 1 ? 0 : 0;
      this.triggerEvent('collectionEvent', status);

    },
    //乡镇详情收藏图标事件
    szsc(e) {
      let index = e.currentTarget.dataset.index;
      this.triggerEvent('scEvent', index)
    },
    //活动日期图标点击事件
    rili() {
      this.triggerEvent('dateEvent', true);
    },
    //发布公共头部返回上一页
    faFui() {
      this.triggerEvent("backEvent")
    },
    //搜索公共头部返回上一页
    searchfaFui() {
      wx.redirectTo({
        url: '/pages/index/index',
      })
    },
    //banner更多详情返回上一页
    gdxqBack() {
      this.triggerEvent('backEvent');
    },
    //用户头像点击事件
    avartTap() {
      wx.navigateTo({
        url: '/pagesWoDe/pages/geRenXinxi/index',
      })
    },
    //路线规划导航栏切换点击事件
    lxNavClick(e) {
      const index = e.currentTarget.dataset.index;
      this.triggerEvent("lxNavEvent", index);
    },
    //商品自定义导航栏返回上一页
    spBack() {
      wx.navigateBack();
    },
  },
})